package cn.zdsoft.configuration.core.api;

import cn.zdsoft.configuration.core.Constants;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * 自定义序列化类
 * Created by wangdongdong on 2018/5/14.
 */
public class CustomSerializer implements ZkSerializer {

    private static final Logger logger = LoggerFactory.getLogger(CustomSerializer.class);

    @Override
    public byte[] serialize(Object o) throws ZkMarshallingError {
        try {
            return String.valueOf(o).getBytes(Constants.charSet);
        } catch (UnsupportedEncodingException e) {
            logger.error("unsupport encoding", e);
            return new byte[0];
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
        try {
            return new String(bytes, Constants.charSet);
        } catch (UnsupportedEncodingException e) {
            logger.error("unsupport encoding", e);
            return new Object();
        }
    }
}
